package com.hod.client.roomtype;

import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.user.client.Window;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtextux.client.data.PagingMemoryProxy;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.frontend.AbstractGrid;

public class RoomTypeGrid extends AbstractGrid {		
    
    private static RecordDef recordDef = new RecordDef(
            new FieldDef[]{
                    new IntegerFieldDef("idRoomType"),
                    new StringFieldDef("name"),
                    new IntegerFieldDef("maxPax"),
                    new FloatFieldDef("price"),
                    new IntegerFieldDef("bed"),
                    new IntegerFieldDef("numberOfRooms")
            }
    );

	

	public RoomTypeGrid() {      
    }

    public RoomTypeGrid(boolean small,GearsDataStore ds) {
    	this.ds = ds;  
    	columns = new BaseColumnConfig[]{
                //new ColumnConfig("idRoomType", "idRoomType", 60, true, null, "idRoomType"),
                new ColumnConfig("name", "name", 60, true, null, "name"),
                new ColumnConfig("maxPax", "maxPax", 60, true, null, "maxPax"),
                new ColumnConfig("price", "price", 60, true, null, "price"),
                //new ColumnConfig("bed", "bed", 60, true, null, "bed"),
                //new ColumnConfig("numberOfRooms", "numberOfRooms", 60, true, null, "numberOfRooms")

        };
    	
    	getData();

        ColumnModel columnModel = getColumnConfigs();
        setColumnModel(columnModel);

        setFrame(true);
        setStripeRows(true);
        setIconCls("grid-icon");
        createPopUp();
        createPager(); 

        
    }
    protected void getData(String id){
    	getData("");
    }
    
    protected void getData(){
    	PagingMemoryProxy proxy = null;
		try {
			GearsQuery q = this.ds.getNamedQuery(RoomType.FIND_ALL);
			RoomType[] data = (RoomType[]) q.execute();
			RoomType rs = null;
			Object[][]data2 = new Object[data.length][];
			for (int i = 0; i < data.length; i++) {
                rs = data[i];	      
                data2[i] = new Object[]{rs.getIdRoomType(),rs.getName(),rs.getMaxPax(),rs.getPrice(),rs.getBed(),rs.getNumberOfRooms()};
            }                        
			proxy = new PagingMemoryProxy(data2);			
			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			Window.alert(e.getMessage());
		} 

        ArrayReader reader = new ArrayReader(recordDef);
        store = new Store(proxy, reader);
        store.load(0,numberRecords);
        setStore(store);
    }
}
